package dice.sinanya.dice.manager;

import dice.sinanya.db.monsterlist.InsertMonsterList;
import dice.sinanya.db.npclist.InsertNpcList;
import dice.sinanya.db.roles.InsertRoles;
import dice.sinanya.db.roles.SelectRoles;
import dice.sinanya.dice.manager.imal.Role;
import dice.sinanya.entity.EntityNickAndRandomAndSkill;
import dice.sinanya.entity.EntityRoleTag;
import dice.sinanya.entity.EntityTypeMessages;
import dice.sinanya.exceptions.ManyRollsTimesTooMoreException;
import dice.sinanya.exceptions.RollCantInZeroException;
import dice.sinanya.tools.getinfo.GetMessagesToValue;
import dice.sinanya.tools.getinfo.MakeRolesInfo;
import dice.sinanya.tools.imal.GetSkillNameFromMuiltRole;
import dice.sinanya.tools.makedata.Calculator;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static dice.sinanya.db.roles.SelectRoles.flushRoleInfoCacheFromDatabase;
import static dice.sinanya.system.MessagesSystem.NONE;
import static dice.sinanya.system.MessagesSystem.SPACE;
import static dice.sinanya.system.MessagesTag.*;
import static dice.sinanya.system.NpcListCache.MONSTER_LIST;
import static dice.sinanya.system.NpcListCache.NPC_LIST;
import static dice.sinanya.system.RoleInfoCache.*;
import static dice.sinanya.tools.checkdata.CheckList.checkMonsterName;
import static dice.sinanya.tools.checkdata.CheckList.checkNpcName;
import static dice.sinanya.tools.getinfo.GetMessagesProperties.entitySystemProperties;
import static dice.sinanya.tools.getinfo.GetNickName.getNickName;
import static dice.sinanya.tools.getinfo.GetNickName.getTrueNickName;
import static dice.sinanya.tools.getinfo.MakeSkillName.makeSkillName;
import static dice.sinanya.tools.getinfo.RoleChoose.*;
import static dice.sinanya.tools.getinfo.RoleInfo.*;
import static dice.sinanya.tools.getinfo.RoleLock.checkRoleLock;
import static dice.sinanya.tools.getinfo.RoleLock.getRoleLockName;
import static dice.sinanya.tools.makedata.GetNickAndRandomAndSkill.getNickAndRandomAndSkill;
import static dice.sinanya.tools.makedata.MakeMessages.deleteTag;
import static dice.sinanya.tools.makedata.Sender.sender;
import static dice.sinanya.tools.makedata.StringFormatById.stringFormatById;
import static java.lang.Integer.min;
import static java.lang.Math.ceil;
import static java.lang.Math.max;

/**
 * @author SitaNya
 * 日期: 2019-06-15
 * 电子邮箱: sitanya@qq.com
 * 维护群(QQ): 162279609
 * 有任何问题欢迎咨询
 * 类说明: 人物卡角色
 */
public class Roles implements Role, GetSkillNameFromMuiltRole {
    private static final Pattern functioin = Pattern.compile("[\\-+*/]");
    private static final Pattern funSep = Pattern.compile("-([^0-9])");
    private final EntityTypeMessages entityTypeMessages;
    Pattern p = Pattern.compile(".*\\d+.*");
    Pattern deliverAt = Pattern.compile("\\[cq:at,qq=([0-9]+).*]");

    public Roles(EntityTypeMessages entityTypeMessages) {
        this.entityTypeMessages = entityTypeMessages;
    }

    private static int findSkillName(int i, String msg, StringBuilder strSkillName) {
        while (i < msg.length() && !Character.isSpaceChar(msg.charAt(i)) &&
                !Character.isDigit(msg.charAt(i)) && !functioin.matcher(String.valueOf(msg.charAt(i))).find() && msg.charAt(i) != '-') {
            strSkillName.append(msg.charAt(i));
            i++;
        }
        return i;
    }

    private static int findSkillValue(int i, String msg, StringBuilder strSkillValue) {
        while (i < msg.length() && (Character.isDigit(msg.charAt(i)) || msg.charAt(i) == '-' || msg.charAt(i) == 'd' || msg.charAt(i) == 'D' || functioin.matcher(String.valueOf(msg.charAt(i))).find())) {
            strSkillValue.append(msg.charAt(i));
            i++;
        }
        return i;
    }

    /**
     * 人物卡变更，“.st角色”视为切换角色
     * “.st角色-属性50”视为修改或添加属性，这取决于是否可以找到这张人物卡，找到的话只修改提及的值，未找到的话其余所有属性置为初始属性
     * 初始属性情况可以参见dice.sinanya.system.MakeRolesInfo
     *
     * @return set是否成功
     */
    @SuppressWarnings("AlibabaMethodTooLong")
    public boolean set() {
        String tag = TAG_ST_SET;
        String msg = (entityTypeMessages.getMsgGet().getMsg().trim().replaceFirst(tag.substring(0, tag.length() - 2), "")).replaceAll("[\r\n]", "").replace("--", "-").replace("－", "-").replace("\t", "").trim();
        String sepRoleAndPro = "-";
        String sepProAndArticle = "\\|";
        String defaultRole = "自定义";
        int lenRoleAndPro = 2;
        String properties;
        String article = null;
        InsertRoles insertRoles = new InsertRoles();
        long qqId = entityTypeMessages.getFromQq();
        String groupId = entityTypeMessages.getFromGroupString();

        if (checkRoleLock(qqId, groupId)) {
            sender(entityTypeMessages, entitySystemProperties.getRoleAlreadyLock(entityTypeMessages.getFromGroupString()));
            return true;
        }
        String role;
        Matcher sepMatcher = funSep.matcher(msg);
        if (sepMatcher.find()) {
            role = msg.split(funSep.toString())[0].trim().toLowerCase();
            if (role.length() > 100) {
                sender(entityTypeMessages, entitySystemProperties.getRoleTooLong(entityTypeMessages.getFromGroupString()));
                return false;
            }
            EntityRoleTag roleTag = new EntityRoleTag(qqId, role);
            if (!role.equals(NONE)) {
                properties = sepMatcher.group(1) + msg.split(funSep.toString())[1];
                if (properties.split(sepProAndArticle).length == lenRoleAndPro) {
                    article = properties.split(sepProAndArticle)[1];
                    article = article.replaceAll(";$", "");
                    properties = properties.split(sepProAndArticle)[0];
                }
                insertRoles.insertRoleInfo(properties, role, qqId, groupId);
                if (article != null) {
                    insertRoles.insertRoleArticle(article, role, qqId);
                }
                if (!ROLE_NAME_CACHE.contains(roleTag)) {
                    ROLE_NAME_CACHE.add(roleTag);
                }
                sender(entityTypeMessages, entitySystemProperties.getSetPropSuccess(entityTypeMessages.getFromGroupString()));
                return true;
            } else {
                return false;
            }
        } else if (!msg.equals(NONE)) {
            role = msg.toLowerCase();
            String nick = getTrueNickName(entityTypeMessages);
            EntityRoleTag roleTag = new EntityRoleTag(qqId, defaultRole);
            if (checkRoleInfoExistByQQ(qqId, groupId, role)) {
                setRoleChooseByQQ(qqId, groupId, role);
                roleTag = new EntityRoleTag(qqId, role);
                if (!ROLE_NAME_CACHE.contains(roleTag)) {
                    ROLE_NAME_CACHE.add(roleTag);
                }
            } else if (defaultRole.equals(role)) {
                ROLE_CHOOSE.put(qqId, defaultRole);
                if (!ROLE_NAME_CACHE.contains(roleTag)) {
                    ROLE_NAME_CACHE.add(roleTag);
                }
                insertRoles.insertRoleInfo("", defaultRole, qqId, groupId);
                sender(entityTypeMessages, stringFormatById(entitySystemProperties.getRoleDefault(entityTypeMessages.getFromGroupString()), nick, msg));
                return true;
            } else if (HasDigit(msg)) {
                if (checkRoleChooseExistByFromQQ(entityTypeMessages) && msg.length() < 200) {
                    insertRoles.insertRoleInfo(msg, getRoleChooseByFromQQ(entityTypeMessages), qqId, groupId);
                    sender(entityTypeMessages, stringFormatById(entitySystemProperties.getRoleUpdate(entityTypeMessages.getFromGroupString()), getRoleChooseByFromQQ(entityTypeMessages)));
                    return true;
                }
                ROLE_CHOOSE.put(qqId, defaultRole);
                if (!ROLE_NAME_CACHE.contains(roleTag)) {
                    ROLE_NAME_CACHE.add(roleTag);
                }
                ROLE_INFO_CACHE.put(new EntityRoleTag(qqId, defaultRole), GetMessagesToValue.getMessagesToValue(msg));
                insertRoles.insertRoleInfo(GetMessagesToValue.getMessagesToValue(msg), defaultRole, qqId);
                sender(entityTypeMessages, stringFormatById(entitySystemProperties.getRoleDefault(entityTypeMessages.getFromGroupString()), nick, nick));
                return true;
            } else {
                ROLE_CHOOSE.put(qqId, defaultRole);
                if (!ROLE_NAME_CACHE.contains(roleTag)) {
                    ROLE_NAME_CACHE.add(roleTag);
                }
                ROLE_TMP_NAME.put(qqId, role);
                insertRoles.deleteRoleTmpName(qqId);
                insertRoles.insertRoleTmpName(qqId, role);
                HashMap<String, Integer> defaultProperties = ROLE_INFO_CACHE.getOrDefault(new EntityRoleTag(qqId, defaultRole), (HashMap<String, Integer>) new MakeRolesInfo().getPropertiesForRole());
                ROLE_INFO_CACHE.put(new EntityRoleTag(qqId, defaultRole), defaultProperties);
                insertRoles.insertRoleInfo(defaultProperties, defaultRole, qqId);
                sender(entityTypeMessages, stringFormatById(entitySystemProperties.getRoleDefault(entityTypeMessages.getFromGroupString()), nick, msg));
                return true;

            }
            sender(entityTypeMessages, stringFormatById(entitySystemProperties.getRoleChange(entityTypeMessages.getFromGroupString()), nick, role));
            return true;
        } else {
            return false;
        }
    }


    public boolean HasDigit(String content) {
        boolean flag = false;
        Matcher m = p.matcher(content);
        if (m.matches()) {
            flag = true;
        }
        return flag;
    }

    /**
     * 返回当前查询QQ的角色列表
     */
    public void list() {
        long qqId = entityTypeMessages.getFromQq();
        String groupId = entityTypeMessages.getFromGroupString();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(entitySystemProperties.getRoleShow(entityTypeMessages.getFromGroupString()));
        if (checkRoleChooseExistByQQ(qqId, groupId)) {
            String role = getRoleChooseByQQ(qqId, groupId);
            if (role.equals("自定义") && ROLE_TMP_NAME.containsKey(qqId)) {
                stringBuilder.append("自定义").append("(").append(ROLE_TMP_NAME.get(qqId)).append(")").append("\n");
            } else {
                stringBuilder.append(role).append("\n");
            }
        } else {
            stringBuilder.append("自定义").append(ROLE_TMP_NAME.getOrDefault(qqId, "")).append("\n");
        }
        stringBuilder.append("当前备选角色:\n");
        StringBuilder standbyRole = new StringBuilder();

        flushRoleInfoCacheFromDatabase();
        for (EntityRoleTag entityRoleTag : ROLE_NAME_CACHE) {
            if (entityRoleTag.getQq() != qqId){
                continue;
            }
            if (checkRoleChooseExistByQQ(qqId, groupId) && !getRoleChooseByQQ(qqId, groupId).equals(entityRoleTag.getRole()) && !entityRoleTag.getRole().contains("自定义")) {
                standbyRole.append(entityRoleTag.getRole()).append("\n");
            }
        }
        if (!stringBuilder.toString().contains("自定义")) {
            standbyRole.append("自定义").append("\n");
        } else if (standbyRole.length() == 0) {
            standbyRole.append("无").append("\n");
        }
        stringBuilder.append(standbyRole);
        String result = stringBuilder.toString();
        sender(entityTypeMessages, result.substring(0, result.length() - 1));
    }

    /**
     * 移除某个角色
     */
    public void move() {
        String tag = TAG_ST_RM;
        String role = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 2));
        String groupId = entityTypeMessages.getFromGroupString();
        long qqId = entityTypeMessages.getFromQq();
        if (checkRoleInfoExistByFromQQ(entityTypeMessages, role)) {
            if (getRoleChooseByQQ(qqId, groupId).equals(role)) {
                if (new SelectRoles().checkRoleChooseLock(String.valueOf(qqId), role)) {
                    sender(entityTypeMessages, entitySystemProperties.getRoleCantDelete(entityTypeMessages.getFromGroupString()));
                } else {
                    new InsertRoles().deleteRoleLockByRole(String.valueOf(qqId), role);
                    sender(entityTypeMessages, entitySystemProperties.getRoleCantDeleteByLock(entityTypeMessages.getFromGroupString()));
                }
            } else {
                removeRoleByQQ(qqId, role);
                ROLE_NAME_CACHE.remove(new EntityRoleTag(qqId, role));
                sender(entityTypeMessages, stringFormatById(entitySystemProperties.getRoleDelete(entityTypeMessages.getFromGroupString()), role));
            }
        } else {
            sender(entityTypeMessages, "未找到输入的角色名");
        }
    }

    /**
     * 显示角色的技能信息
     */
    public void show() {
        String tag = TAG_ST_SHOW;
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 2));
        String qq = entityTypeMessages.getFromQqString();
        if (!msg.equals(NONE)) {
            if (checkRoleChooseExistByFromQQ(entityTypeMessages)) {
                HashMap<String, Integer> properties = (HashMap<String, Integer>) getRoleInfoFromChooseByFromQQ(entityTypeMessages);
                sender(entityTypeMessages, msg + "\t:\t" + properties.getOrDefault(makeSkillName(msg), 0));
                return;
            }
        }
        StringBuilder stringBuilder = showProp(qq, entityTypeMessages.getFromGroupString());
        stringBuilder.append(showArticle(qq, entityTypeMessages.getFromGroupString()));
        sender(entityTypeMessages, stringBuilder.toString());
    }

    /**
     * 显示角色的技能信息
     */
    public void get() {
        String qq = entityTypeMessages.getFromQqString();
        String groupId = entityTypeMessages.getFromGroupString();
        StringBuilder stringBuilder = getProp(qq, groupId);
        sender(entityTypeMessages, stringBuilder.toString());
    }

    public void change() throws ManyRollsTimesTooMoreException, RollCantInZeroException {
        String tag = TAG_ST_CHANGE;
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 2));
        String groupId = entityTypeMessages.getFromGroupString();
        long qqId = entityTypeMessages.getFromQq();
        StringBuilder restult = new StringBuilder();
        StringBuilder strSkillValue = new StringBuilder();
        StringBuilder strSkillName = new StringBuilder();
        int i = 0;
        if (msg.contains(":")) {
            String nick = msg.split(":")[0];
            msg = msg.split(":")[1];

            if (checkRoleInfoExistByQQ(qqId, groupId, nick)) {
                HashMap<String, Integer> properties = (HashMap<String, Integer>) getRoleInfoByQQ(qqId, groupId, nick);
                restult.append(makeNickToSender(nick)).append("的技能变化情况为");

                int con = properties.getOrDefault("con", 0);
                int siz = properties.getOrDefault("siz", 0);
                int cthulhuMythos = properties.getOrDefault("cthulhuMythos", 0);
                int hpc = properties.getOrDefault("hpc", 0);

                while (i < msg.length()) {
                    int tmp = i;
                    i = findSkillName(i, msg, strSkillName);
                    i = findSkillValue(i, msg, strSkillValue);

                    if (!strSkillValue.toString().equals(NONE)) {
                        String skillName = GetSkillNameFromMuiltRole.getSkillNameFromMuiltRole(strSkillName);
                        String tmp2 = nick + ":" + skillName;
                        EntityNickAndRandomAndSkill entityNickAndRandomAndSkill = getNickAndRandomAndSkill(entityTypeMessages, tmp2);
                        assert properties != null;
                        String addString = (nick + ":" + msg).replace(tmp2, "");
                        Matcher addType = functioin.matcher(addString);
                        int result;
                        if (addType.find()) {
                            result = (int) (ceil(Calculator.conversion(entityNickAndRandomAndSkill.getSkill() + addString)));
                        } else {
                            result = (int) (ceil(Calculator.conversion(addString)));
                        }
                        if (makeSkillName(skillName).equals("hp")) {
                            int maxHp = (con + siz) / 10 + hpc;
                            result = min(maxHp, result);
                            result = max(0, result);
                        }

                        if (makeSkillName(skillName).equals("san")) {
                            int maxSan = 99 - cthulhuMythos;
                            result = min(maxSan, result);
                            result = max(0, result);
                        }
                        properties.put(makeSkillName(skillName), result);
                        restult.append("\n").append(skillName).append(": ").append(strSkillValue).append(" --> ").append(result);
                        strSkillName = new StringBuilder();
                        strSkillValue = new StringBuilder();
                    }
                    if (tmp == i) {
                        i++;
                    }
                }
                setRoleInfoByFromQQ(entityTypeMessages, nick, properties);
                sender(entityTypeMessages, restult.toString());
            }
            if (checkMonsterName(groupId, nick) || checkNpcName(groupId, nick)) {
                HashMap<String, Integer> properties = MONSTER_LIST.getOrDefault(groupId, new HashMap<>()).get(nick);
                if (properties == null) {
                    properties = NPC_LIST.getOrDefault(groupId, new HashMap<>()).get(nick);
                }
                restult.append(makeNickToSender(nick)).append("的技能变化情况为");

                while (i < msg.length()) {
                    int tmp = i;
                    i = findSkillName(i, msg, strSkillName);
                    i = findSkillValue(i, msg, strSkillValue);

                    if (!strSkillValue.toString().equals(NONE)) {
                        String skillName = GetSkillNameFromMuiltRole.getSkillNameFromMuiltRole(strSkillName);
                        String tmp2 = nick + ":" + skillName;
                        EntityNickAndRandomAndSkill entityNickAndRandomAndSkill = getNickAndRandomAndSkill(entityTypeMessages, tmp2);
                        assert properties != null;
                        String addString = (nick + ":" + msg).replace(tmp2, "");
                        Matcher addType = functioin.matcher(addString);
                        int result;
                        if (addType.find()) {
                            String funStr = entityNickAndRandomAndSkill.getSkill() + addString;
                            result = (int) (ceil(Calculator.conversion(funStr.replaceAll(" +", ""))));
                        } else {
                            result = (int) (ceil(Calculator.conversion(addString)));
                        }

                        properties.put(makeSkillName(skillName), result);
                        restult.append("\n").append(skillName).append(": ").append(strSkillValue).append(" --> ").append(result);
                        strSkillName = new StringBuilder();
                        strSkillValue = new StringBuilder();
                    }
                    if (tmp == i) {
                        i++;
                    }
                }
                if (MONSTER_LIST.containsKey(groupId)) {
                    new InsertMonsterList().insertMonsterList(groupId, nick, properties);
                } else if (NPC_LIST.containsKey(groupId)) {
                    new InsertNpcList().insertNpcList(groupId, nick, properties);
                }
                sender(entityTypeMessages, restult.toString());
            }
        } else {
            if (checkRoleChooseExistByFromQQ(entityTypeMessages)) {
                HashMap<String, Integer> properties = (HashMap<String, Integer>) getRoleInfoFromChooseByFromQQ(entityTypeMessages);
                restult.append("您的技能变化情况为");
                int con = properties.getOrDefault("con", 0);
                int siz = properties.getOrDefault("siz", 0);
                int cthulhuMythos = properties.getOrDefault("cthulhuMythos", 0);
                int hpc = properties.getOrDefault("hpc", 0);
                while (i < msg.length()) {
                    int tmp = i;
                    i = findSkillName(i, msg, strSkillName);
                    i = findSkillValue(i, msg, strSkillValue);

                    if (!strSkillValue.toString().equals(NONE)) {
                        String skillName = GetSkillNameFromMuiltRole.getSkillNameFromMuiltRole(strSkillName);
                        EntityNickAndRandomAndSkill entityNickAndRandomAndSkill = getNickAndRandomAndSkill(entityTypeMessages, skillName + strSkillValue);
                        assert properties != null;
                        int result = entityNickAndRandomAndSkill.getSkill();
                        if (makeSkillName(skillName).equals("hp")) {
                            int maxHp = (con + siz) / 10 + hpc;
                            result = min(maxHp, result);
                            result = max(0, result);
                        }

                        if (makeSkillName(skillName).equals("san")) {
                            int maxSan = 99 - cthulhuMythos;
                            result = min(maxSan, result);
                            result = max(0, result);
                        }
                        properties.put(makeSkillName(skillName), result);
                        restult.append("\n").append(skillName).append(": ").append(strSkillValue).append(" --> ").append(result);
                        strSkillName = new StringBuilder();
                        strSkillValue = new StringBuilder();
                    }
                    if (tmp == i) {
                        i++;
                    }
                }
                setRoleInfoFromChooseByQQ(qqId, groupId, properties);
                sender(entityTypeMessages, restult.toString());
            }
        }
    }


    public void clr() {
        long qqId = entityTypeMessages.getFromQq();
        String defaultRole = "自定义";
        String nick = getTrueNickName(entityTypeMessages);
        EntityRoleTag roleTag = new EntityRoleTag(qqId, defaultRole);

        ROLE_CHOOSE.put(qqId, defaultRole);
        if (!ROLE_NAME_CACHE.contains(roleTag)) {
            ROLE_NAME_CACHE.add(roleTag);
        }
        ROLE_TMP_NAME.remove(qqId);
        ROLE_INFO_CACHE.remove(roleTag);
        InsertRoles insertRoles = new InsertRoles();
        insertRoles.deleteInfo(qqId, defaultRole);
        insertRoles.deleteRoleTmpName(qqId);
        insertRoles.insertRoleInfo("", defaultRole, qqId, entityTypeMessages.getFromGroupString());
        sender(entityTypeMessages, stringFormatById(entitySystemProperties.getRoleChangeDefault(entityTypeMessages.getFromGroupString()), nick));
    }

    public void lock() {
        long qqId = entityTypeMessages.getFromQq();
        String groupId = entityTypeMessages.getFromGroupString();
        String role = "";
        if (checkRoleChooseExistByFromQQ(entityTypeMessages)) {
            role = getRoleChooseByFromQQ(entityTypeMessages);
        }
        if (checkRoleLock(qqId, groupId)) {
            sender(entityTypeMessages, stringFormatById(entitySystemProperties.getRoleLock(entityTypeMessages.getFromGroupString()), getRoleLockName(qqId, groupId, role)));
        } else {
            new InsertRoles().insertRoleLock(String.valueOf(qqId), groupId, role);
            sender(entityTypeMessages, stringFormatById(entitySystemProperties.getRoleLockDone(entityTypeMessages.getFromGroupString()), role));
        }
    }

    public void unlock() {
        long qqId = entityTypeMessages.getFromQq();
        String groupId = entityTypeMessages.getFromGroupString();
        String role = "";
        if (checkRoleLock(qqId, groupId)) {
            sender(entityTypeMessages, stringFormatById(entitySystemProperties.getRoleUnlock(entityTypeMessages.getFromGroupString()), getRoleLockName(qqId, groupId, role)));
            new InsertRoles().deleteRoleLock(String.valueOf(qqId), groupId);
        } else {
            sender(entityTypeMessages, entitySystemProperties.getRoleNotLock(entityTypeMessages.getFromGroupString()));
        }
    }

    public void rename() {
        String tag = TAG_ST_RENAME;
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 2));
        String groupId = entityTypeMessages.getFromGroupString();
        String oldName;
        String defaultRole = "自定义";
        long qqId = entityTypeMessages.getFromQq();
        if (checkRoleChooseExistByFromQQ(entityTypeMessages)) {
            oldName = getRoleChooseByQQ(qqId, groupId);
            if (oldName.equals(defaultRole)) {
                HashMap<String, Integer> defaultProperties = ROLE_INFO_CACHE.getOrDefault(new EntityRoleTag(qqId, defaultRole), (HashMap<String, Integer>) new MakeRolesInfo().getPropertiesForRole());
                StringBuilder prop = new StringBuilder(".st");
                prop.append(msg).append("-");
                for (Map.Entry<String, Integer> map : defaultProperties.entrySet()) {
                    prop.append(map.getKey()).append(map.getValue());
                }
                entityTypeMessages.getMsgGet().setMsg(prop.toString());
                set();
                clr();
                entityTypeMessages.getMsgGet().setMsg(".st" + msg);
                set();
            } else {
                new InsertRoles().renameRole(String.valueOf(qqId), groupId, oldName, msg);
            }
            sender(entityTypeMessages, stringFormatById(entitySystemProperties.getRoleChangeName(entityTypeMessages.getFromGroupString()), oldName, msg));
        } else {
            sender(entityTypeMessages, entitySystemProperties.getRoleNotChoose(entityTypeMessages.getFromGroupString()));
        }
    }

    public void deliver() {
        String tag = TAG_ST_DELIVER;
        String msg = deleteTag(entityTypeMessages.getMsgGet().getMsg(), tag.substring(0, tag.length() - 2));
        Matcher deliverAtMatcher = deliverAt.matcher(msg);
        InsertRoles insertRoles = new InsertRoles();
        if (deliverAtMatcher.find()) {
            long qqId = Long.parseLong(deliverAtMatcher.group(1));
            String roleName = msg.split(SPACE)[0];
            if (checkRoleInfoExistByQQNotLock(entityTypeMessages.getFromQq(), roleName)) {
                HashMap<String, Integer> roleProp = (HashMap<String, Integer>) getRoleInfoByQQNotLock(entityTypeMessages.getFromQq(), entityTypeMessages.getFromGroupString(), roleName);
                insertRoles.insertRoleInfo(roleProp, roleName, qqId);
                //TODO
                sender(entityTypeMessages, stringFormatById("%s 将人物卡 %s 交于 %s", getNickName(entityTypeMessages), roleName, getNickName(qqId, entityTypeMessages)));
            }
        } else {
            //TODO
            sender(entityTypeMessages, "格式输入有误，请按照.st cp 角色名 [at对方]进行发送");
        }
    }
}
